import 'dart:convert';

import 'package:easy_refresh/easy_refresh.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/services.dart';
import 'package:flutter_base_app/export.dart';
import 'package:flutter_base_app/generated/assets.dart';
import 'package:flutter_base_app/utils/channel/app_type.dart';
import 'package:flutter_common_utils/common_utils_export.dart';
import 'package:flutter_common_utils/mmkv_export.dart';

import '../config/theme.dart';
import 'http/http_manager.dart';
class BaseAppUtils {
  BaseAppUtils._();
  static Map<String, String> mccMncMap = {};

  static Future<void> init() async {
    await AppUtils.init();
    await AppType.init();
    await StoreUtils.init(MMKVStore());
    await ThemeUtils.init();
    await _initMccMncMap();
    await FileUtils.init();
    HttpManager.init();
    AppLifecycleManager().init();
    try {
      // 读取 channel_id
      //flutter build windows --dart-define=channel_id=123 --dart-define=api_key=your_api_key_here
      // const String? channelId = String.fromEnvironment('channel_id');
      // LogUtils.e("wwwww ${channelId}");
    } catch (e) {
      LogUtils.e("Error loading channel ID: $e");
    }
    EasyRefresh.defaultFooterBuilder = () => AppRefresherFooter(
      readyText: I18nKey.loading.trx,
      processingText: I18nKey.loading.trx,
      processedText: I18nKey.successful.trx,
      noMoreText: I18nKey.noData.trx,
      failedText: I18nKey.failed.trx,
      showMessage: false,
    );
    EasyRefresh.defaultHeaderBuilder = () => const AppRefresherHeader();
  }

  static Future<void> _initMccMncMap() async {
    final jsonString = await rootBundle.loadString(Assets.jsonMccMnc);
    mccMncMap = json.decode(jsonString).cast<String, String>();
  }

  static Future<FilePickerResult?> pickFile({List<String>? allowedExtensions}) {
    return FilePicker.platform.pickFiles(
      type: allowedExtensions.isNullOrEmpty ? FileType.any : FileType.custom,
      allowedExtensions: allowedExtensions,
      lockParentWindow: true,
    );
  }
}
